package com.example.exam.user.dao;

import com.example.exam.db.BaseDao;
import com.example.exam.user.model.UserRoleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserRoleDao extends BaseDao<UserRoleBean, Integer> {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String findByIdSql() {
		return "select * from sys_user_role where id = ?";
	}
	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getAddSql(){
		return "insert into sys_user_role (id, user_id, role_id, state, create_user, create_time) values (:id, :userId, :roleId, :state, :createUser, :createTime) ";
	}
	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getUpdateAllFieldSql(){
		return "update sys_user_role set user_id=:userId, role_id=:roleId, state=:state, create_user=:createUser, create_time=:createTime where id=:id ";
	}

	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getRemoveByIdSql() {
		return "update sys_user_role set status = -1 where id = ? ";
	}
	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getDeleteByIdSql(){
		return "delete from sys_user_role where id = ? ";
	}
	/**
	 * @Description:
	 * @Param: [userId]
	 * @return: java.lang.Integer
	 * @Author: xhw
	 * @Date: 2021/7/26
	 */
	public Integer deleteByUserId(Integer userId) {
		String sql  = "DELETE FROM sys_user_role WHERE user_id=:userId";
		UserRoleBean condition = new UserRoleBean();
		condition.setUserId(userId);
		return updateByCondition(sql, condition);
	}
	/**
	 * @Description:
	 * @Param: [userId]
	 * @return: java.util.List<com.example.exam.user.model.UserRoleBean>
	 * @Author: xhw
	 * @Date: 2021/7/26
	 */
	public List<UserRoleBean> findByUserId(Integer userId) {
		String sql = "select * from sys_user_role where user_id=?";
		List<Object> listParam=new ArrayList<>();
		listParam.add(userId);
		try{
			List<UserRoleBean> list=getJtl().query(sql,new BeanPropertyRowMapper<UserRoleBean>(UserRoleBean.class),listParam.toArray());
			return  list;
		}catch (Exception e){
			return null;
		}
	}/**
	 * @Description:
	 * @Param: [id]
	 * @return: java.util.List
	 * @Author: xhw
	 * @Date: 2021/7/26
	 */
	public List authorization(Integer id){
		try{
		String sql="SELECT CONCAT_WS('|', m.modelName, m.perName) AS pre FROM" +
				" (SELECT DISTINCT(SELECT r.permission FROM sys_resource r WHERE r.status=1 AND r.id=t.resource_id)modelName," +
				" (SELECT p.coding FROM sys_permission p WHERE p.state=1 AND p.id=t.permission_id) perName" +
				" FROM sys_role_resource_permission t WHERE t.state=1 AND  t.role_id" +
				" IN(SELECT ur.role_id FROM sys_user_role  ur WHERE ur.state=1 AND ur.user_id=?)) m";
		List<Object> listParam=new ArrayList<>();
		listParam.add(id);
		List<UserRoleBean> list=getJtl().query(sql,new BeanPropertyRowMapper<UserRoleBean>(UserRoleBean.class),listParam.toArray());
		//System.out.println("joso串"+JSON.toJSONString(list.get(0)));
		return list;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}
 }  